package com.project.service.impl;

import com.project.mapper.ReportAnnotationMapper;
import com.project.model.entity.ReportAnnotation;
import com.project.service.ReportAnnotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReportAnnotationServiceImpl implements ReportAnnotationService {
    private final ReportAnnotationMapper reportAnnotationMapper;

    @Autowired
    public ReportAnnotationServiceImpl(ReportAnnotationMapper reportAnnotationMapper) {
        this.reportAnnotationMapper = reportAnnotationMapper;
    }

    @Override
    public ReportAnnotation addReportAnnotation(ReportAnnotation reportAnnotation) {
        reportAnnotationMapper.insertReportAnnotation(reportAnnotation);
        return reportAnnotation;
    }

    @Override
    public void deleteReportAnnotation(Integer id) {
        reportAnnotationMapper.deleteReportAnnotationById(id);
    }

    @Override
    public ReportAnnotation updateReportAnnotation(ReportAnnotation reportAnnotation) {
        reportAnnotationMapper.updateReportAnnotation(reportAnnotation);
        return reportAnnotation;
    }

    @Override
    public ReportAnnotation getReportAnnotationById(Integer id) {
        return reportAnnotationMapper.selectReportAnnotationById(id);
    }

    @Override
    public List<ReportAnnotation> getAllReportAnnotations() {
        return reportAnnotationMapper.selectAllReportAnnotations();
    }

    @Override
    public List<ReportAnnotation> getReportAnnotationsByReportId(Integer reportId) {
        return reportAnnotationMapper.selectReportAnnotationsByReportId(reportId);
    }

    @Override
    public ReportAnnotation saveOrUpdateAnnotation(ReportAnnotation reportAnnotation) {
        ReportAnnotation annotation;
        if(getReportAnnotationById(reportAnnotation.getId())==null){
            annotation=addReportAnnotation(reportAnnotation);
        }else {
            annotation=updateReportAnnotation(reportAnnotation);
        }
        return annotation;
    }

    @Override
    public ReportAnnotation getReviewingAnnotationByReportId(Integer reportId) {
        List<ReportAnnotation> annotations=reportAnnotationMapper.selectReportAnnotationsByReportId(reportId);
        for(ReportAnnotation annotation:annotations){
            if(annotation.getStatus().equals("未完成")){
                return annotation;
            }
        }
        return null;
    }
}
